package com.wanxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.LawyerModel;
import com.wanxi.model.UserModel;
import com.wanxi.service.LawyerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Li
 * @Date 2023/7/7 19:58
 */
@RestController
@Api(tags = "律师管理", value = "用户Open api 入口")
@RequestMapping("/back/lawyer")
public class LawyerController {
    @Autowired
    private LawyerService lawyerService;

    @GetMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询所有律师信息", response = LawyerModel.class, httpMethod = "GET")
    public ResultDto findAll(LawyerModel lawyerModel) {
        IPage<LawyerModel> page = new Page<>(lawyerModel.getPage(), lawyerModel.getLimit());
//        QueryWrapper<LawyerModel> queryWrappers = query(lawyerModel);
        IPage<LawyerModel> iPage = lawyerService.page(page, null);
        return ResultDto.builder().code(0).data(iPage.getRecords()).count((int) iPage.getTotal()).build();
    }

    @GetMapping("/findEs")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    public ResultDto findEs(LawyerModel lawyerModel) throws IOException { ;
        lawyerModel.getIntroduce();
        List<Map<String, Object>> lawyer = lawyerService.searchLawyer(lawyerModel);
        return ResultDto.builder().code(0).data(lawyer).count(lawyer.size()).build();
    }

    @PostMapping("/addLawyer")
    @PreAuthorize("hasAnyAuthority('add')")//权限
    @ApiOperation(value = "新增律师信息", response = LawyerModel.class, httpMethod = "POST")
    public ResultDto addLawyer(LawyerModel lawyerModel) throws IOException {
        lawyerService.save(lawyerModel);
        lawyerService.addDocument(lawyerModel);
        return ResultDto.builder()
                .code(0)
                .msg("新增成功！")
                .build();
    }

    @PostMapping("/updateLawyer")
    @PreAuthorize("hasAnyAuthority('update')")//权限
    @ApiOperation(value = "修改律师信息", response = LawyerModel.class, httpMethod = "POST")
    public ResultDto updateLawyer(LawyerModel lawyerModel) throws IOException {
        lawyerService.updateById(lawyerModel);
        lawyerService.updateDocument(lawyerModel);
        return ResultDto.builder()
                .code(0)
                .msg("修改成功！")
                .build();
    }

    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "根据id查询律师信息", response = LawyerModel.class, httpMethod = "GET")
    public ResultDto findById(LawyerModel lawyerModel) {
        LawyerModel lawyers = lawyerService.getById(lawyerModel.getId());
        return ResultDto.builder().data(lawyers).build();
    }

    @PostMapping("/deleteLawyer")
    @PreAuthorize("hasAnyAuthority('delete')")//权限
    @ApiOperation(value = "删除律师信息", response = LawyerModel.class, httpMethod = "POST")
    public ResultDto deleteLawyer(LawyerModel lawyerModel) throws IOException {
        lawyerService.removeById(lawyerModel.getId());
        lawyerService.deleteDocument(lawyerModel);
        return ResultDto.builder()
                .code(0)
                .msg("删除成功")
                .build();
    }

    public QueryWrapper<LawyerModel> query(LawyerModel lawyerModel) {
        QueryWrapper<LawyerModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(lawyerModel.getName() == null), "name", lawyerModel.getName());
        queryWrapper.like(!(lawyerModel.getPhone() == null), "introduce", lawyerModel.getIntroduce());
        return queryWrapper;
    }
}
